#include "fepch.h"
#include "SolidBody.h"
#include "FysicsEngine/Imgui/EditorUI.h"
#include "FysicsEngine/Renderer/Renderer.h"
#include "FysicsEngine/Application.h"

namespace Fysics
{
	SolidBody::SolidBody(glm::vec3 Position, glm::vec3 Rotation, glm::vec3 Scale, bool isVisible)
		: Item("SolidBody", Position), m_Rotation(Rotation), m_Scale(Scale), m_Visible(isVisible)
	{
	}

	void SolidBody::SetRotation(glm::vec3 rotation)
	{
		m_Rotation = rotation;
	}

	void SolidBody::SetScale(glm::vec3 scale)
	{
		m_Scale = scale;
	}

	void SolidBody::SetRotX(float x)
	{
		m_Rotation.x = x;
	}

	void SolidBody::SetRotY(float y)
	{
		m_Rotation.y = y;
	}

	void SolidBody::SetRotZ(float z)
	{
		m_Rotation.z = z;
	}

	void SolidBody::SetScaleX(float x)
	{
		m_Scale.x = x;
	}

	void SolidBody::SetScaleY(float y)
	{
		m_Scale.y = y;
	}

	void SolidBody::SetScaleZ(float z)
	{
		m_Scale.z = z;
	}

	glm::mat4 SolidBody::GetModelMatrix() const
	{
		glm::mat4 model = glm::mat4(1.0f);
		glm::mat4 transform = glm::translate(model, m_Position)
			* glm::rotate(model, glm::radians(m_Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f))
			* glm::rotate(model, glm::radians(m_Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f))
			* glm::rotate(model, glm::radians(m_Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f))
			* glm::scale(model, m_Scale);
		return transform;
	}

	void SolidBody::Draw()
	{
		Application& app = Application::Get();
		Renderer::PushObject(this);
		Renderer::Submit(app.GetShader(SHADER_LIGHT), app.GetVertexArray(VA_BOX_LIGHT));
	}

	void SolidBody::ShowEditPanel()
	{
		EditorUI::Text(GetName());
		static char name[64] = { 0 };
		EditorUI::InputBox("Name", name, 64);
		EditorUI::SetSameLine();
		if (EditorUI::Button("Comfirm", nullptr, 60, 20) && name[0] != '\0')
		{
			SetName(std::string(name));
			name[0] = '\0';
		}
		if (EditorUI::TreeNode("Position"))
		{
			EditorUI::DragFloat("Position x", &m_Position.x, -50.f, 50.f);
			EditorUI::DragFloat("Position y", &m_Position.y, -50.f, 50.f);
			EditorUI::DragFloat("Position z", &m_Position.z, -50.f, 50.f);
			EditorUI::TreePop();
		}	

		if (EditorUI::TreeNode("Rotation"))
		{
			EditorUI::SliderFloat("Rotation x", &m_Rotation.x, -180.f, 180.f);
			EditorUI::SliderFloat("Rotation y", &m_Rotation.y, -180.f, 180.f);
			EditorUI::SliderFloat("Rotation z", &m_Rotation.z, -180.f, 180.f);
			EditorUI::TreePop();
		}
		
		if (EditorUI::TreeNode("Scale"))
		{
			EditorUI::SliderFloat("Scale x", &m_Scale.x, -10.f, 10.f);
			EditorUI::SliderFloat("Scale y", &m_Scale.y, -10.f, 10.f);
			EditorUI::SliderFloat("Scale z", &m_Scale.z, -10.f, 10.f);
			EditorUI::TreePop();
		}
		
	}
}